package com.ehome.poct.base.controller.device;

import com.ehome.poct.base.api.dto.device.QrCodeHisDetailDTO;
import com.ehome.poct.base.api.dto.device.TestDeviceQrcodePrintHisDTO;
import com.ehome.poct.base.api.dto.device.TestDeviceQrcodePrintHisHeadDTO;
import com.ehome.poct.base.api.virtual.TestDeviceQrcodePrintHisHeadService;
import com.ehome.poct.base.api.virtual.TestDeviceQrcodePrintHisService;
import com.ehome.poct.base.api.vo.device.QrcodePrintHisDetPageVO;
import com.ehome.poct.base.api.vo.device.QrcodePrintHisPageVO;
import com.ehome.poct.base.api.vo.device.QrcodePrintHisDetVO;
import com.ehome.web.controller.AbstractBaseController;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 打印记录详细控制器
 *
 * @author ehome
 * @Date 2022-07-14 15:54
 */
@Slf4j
@Api(tags = "打印记录详细")
@RestController
@RequestMapping("/qrcodeprinthisdetail")
public class TestDeviceQrcodePrintHisController extends AbstractBaseController<QrcodePrintHisDetVO, TestDeviceQrcodePrintHisDTO> {

    @Autowired
    private TestDeviceQrcodePrintHisService qrcodePrintHisService;
    @Autowired
    private TestDeviceQrcodePrintHisHeadService qrcodePrintHisHeadService;

    @ApiOperation("增加打印记录")
    @ApiOperationSupport(author = "cq")
    @PostMapping("/addQrcodePrintHis")
    public Boolean addQrcodePrintHis(@RequestBody TestDeviceQrcodePrintHisDTO dto){
        TestDeviceQrcodePrintHisDTO d= qrcodePrintHisService.insert(dto);
        TestDeviceQrcodePrintHisHeadDTO thd = new TestDeviceQrcodePrintHisHeadDTO();
        thd.setPrintDate(d.getPrintDate());
        var t = qrcodePrintHisHeadService.selectOne(thd);
        if(null==t){
            thd.setNum(1);
            qrcodePrintHisHeadService.insert(thd);
        }else
        {
            t.setNum(t.getNum()+1);
            t.setUpdateTime(LocalDateTime.now());
            qrcodePrintHisHeadService.update(t.getTestDeviceQrcodePrintHisHeadId(),t);
        }
          return true;
    }


    @ApiOperation("获取打印记录详细列表")
    @ApiOperationSupport(author = "cq")
    @PostMapping("/getlist")
    public QrcodePrintHisDetPageVO getlist(@RequestBody QrCodeHisDetailDTO req)
    {
        return qrcodePrintHisService.getList(req);
    }
}
